package cn.wsn2018.tools.exceptionHandler;

import cn.wsn2018.tools.common.CommonValue;
import cn.wsn2018.tools.model.Log.ErrorLogInfo;
import cn.wsn2018.tools.util.DateUtil;
import cn.wsn2018.tools.util.LogUtil;
import cn.wsn2018.tools.util.ServletUtil;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Random;

/**
 * @program: tools
 * @description
 * @author: songDaisong
 * @create: 2019-12-07 18:34
 **/

@RestControllerAdvice
public class GlobalDefaultException {

    @ExceptionHandler(value = Exception.class)
    public void catchException(Exception e){

//        String errmessage = ExceptionUtil.getErrmessage(e);
//        System.out.println("========================================================================");
//        System.out.println("===ERRORINFO===:"+errmessage );
//        System.out.println("========================================================================");

        //System.out.println("@@@@@@@@:"+e.getClass().getName());
        //System.out.println("@@@@@@@@type:"+e.getClass().getTypeName());


        String ipAddress ="127.0.0.1";
        String machineName="localhost";
        try {
            ipAddress =InetAddress.getLocalHost().getHostAddress();
            machineName = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException ex) {
            ex.printStackTrace();
        }finally {
//           LoginUser loginUser =(LoginUser) ServletUtil.getUserMap().get(Thread.currentThread().getId());
            Random random = new Random();

            String[] strArray={
                    CommonValue.LOGINMODULE,
                    CommonValue.DESIGNMODULE,
                    CommonValue.TESTMODULE,
                    CommonValue.BROWSEMODULE,
                    CommonValue.ERRLOGMODULE,
                    CommonValue.LOGMODULE,
                    CommonValue.WARNINGLOGMODULE
            };
            int nextInt = random.nextInt(strArray.length);

            ErrorLogInfo errorLogInfo = new ErrorLogInfo();
            errorLogInfo.setType(CommonValue.LOG_TYPE_0);
            errorLogInfo.setCreateTime(new DateUtil().dateToString(new Date()));
            errorLogInfo.setErrorRunningInfo(e.getClass().getName());

//          errorLogInfo.setModuleName(loginUser.getModuleName());
//          errorLogInfo.setUserName(loginUser.getUsername());
            errorLogInfo.setModuleName(CommonValue.DESIGNMODULE);
            errorLogInfo.setUserName("100010");

            errorLogInfo.setMachineName(machineName);
            errorLogInfo.setIpAddress(ipAddress);

           // errorLogInfo.setOperatorType(e.getClass().getSimpleName());
            errorLogInfo.setOperatorType(ServletUtil.operatorTypeMap.get(CommonValue.DESIGNMODULE));

            LogUtil.produceErrorLogs(errorLogInfo);
        }




    }
}
